package com.zenagro.actions.crud;

import java.util.*;

import javax.inject.*;

import org.openxava.actions.*;
import org.openxava.model.*;
import org.openxava.validators.*;

import com.zenagro.utils.*;

public class DeleteFileSelectedAction extends TabBaseAction implements IModelAction {

	@Inject HashMap mapaComun;
	
	private String model;
	
	public void execute() throws Exception {	
		
		FileEntityUtils feu= new FileEntityUtils(getView(),mapaComun);
		
		Map [] selectedOnes = getSelectedKeys(); 
		if (selectedOnes != null) {						
			for (int i = 0; i < selectedOnes.length; i++) {				
				Map key = selectedOnes[i];
				try {	
					
					//Objetner fichero y borrar despues
					String filename= feu.getFileName(key);
					MapFacade.remove(model, key);
					if(filename!=null)
					feu.deleteFolder(filename);
				}
				catch (ValidationException ex) {
					addError("no_delete_row", new Integer(i), key);
					addErrors(ex.getErrors());
				}								
				catch (Exception ex) { 
					addError("no_delete_row", new Integer(i), key);
									}						
			}
			getTab().deselectAll();
			resetDescriptionsCache();
		}
	}

	public void setModel(String modelName) {
		this.model = modelName;		
	}
}
